package com.ziyun.erp.web.api.order;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ziyun.erp.common.utils.JsonUtils;
import com.ziyun.erp.modules.order.dto.OrderAllotDto;
import com.ziyun.erp.modules.order.dto.OrderItemAllotDto;
import com.ziyun.erp.modules.order.entity.ErpOrderEntity;
import com.ziyun.erp.modules.order.entity.ErpOrderLogEntity;
import com.ziyun.erp.modules.order.entity.OrderRemarksEntity;
import com.ziyun.erp.modules.order.service.ErpOrderLogService;
import com.ziyun.erp.modules.order.service.ErpOrderService;
import com.ziyun.erp.modules.order.service.OrderRemarksService;
import com.ziyun.erp.modules.production.entity.ErpOrderItemEntity;
import com.ziyun.erp.modules.production.service.ErpOrderItemService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * erp订单信息表
 *
 * @author llc
 * @date 2018-09-21 11:19:18
 */
@RestController
@RequestMapping("port/order/erporder")
public class ErpOrderControllerApi {

    @Autowired
    private ErpOrderService erpOrderService;
    @Autowired
    private ErpOrderLogService erpOrderLogService;
    @Autowired
    private OrderRemarksService orderRemarksService;
    @Autowired
    private ErpOrderItemService erpOrderItemService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        return erpOrderService.queryPageErpOrderList(params);
    }

    /**
     * 列表
     */
    @RequestMapping("/erpOrderlist")
    public R erpOrderlist(@RequestParam Map<String, Object> params){
        return erpOrderService.queryPage(params);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{orderNo}")
    public R info(@PathVariable("orderNo") String orderNo){
        R re = R.ok();
        // 订单信息
        ErpOrderEntity erpOrder =  erpOrderService.selectByNo(orderNo);
        re.put("erpOrder", erpOrder);

        // 客服备注
        List<OrderRemarksEntity> remarkList = orderRemarksService.selectList(
                new EntityWrapper<OrderRemarksEntity>()
                        .eq("or_del", "0")
                        .eq("or_order_id", erpOrder.getOriginalOrderId())
        );
        re.put("remarkList", remarkList);

        // 商品列表
        List<ErpOrderItemEntity> erpOrderItemEntityList = erpOrderItemService.selectList(
                new EntityWrapper<ErpOrderItemEntity>()
                .eq("order_no",orderNo)
                .eq("item_del",0)
        );
        re.put("erpOrderItemEntityList", erpOrderItemEntityList);

        return re;
    }

    /**
     * 订单分配页面信息
     * @param orderNo
     * @return
     */
    @RequestMapping("/allotInfo/{orderNo}")
    public R allotInfo(@PathVariable("orderNo") String orderNo){
        return erpOrderService.allotInfo(orderNo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ErpOrderEntity erpOrder){
        erpOrderService.insert(erpOrder);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ErpOrderEntity erpOrder){
        ValidatorUtils.validateEntity(erpOrder);
        erpOrderService.updateById(erpOrder);
        
        return R.ok();
    }

    /**
     * 修改收货人信息
     */
    @RequestMapping("/orderReceiverUpdate")
    public R orderReceiverUpdate(@RequestBody Map<String, Object> paramBodyMap){
        ErpOrderEntity erpOrder=JsonUtils.string2Object(paramBodyMap.get("erpOrder").toString(),ErpOrderEntity.class);
        boolean result = erpOrderService.updateById(erpOrder);
        String username=paramBodyMap.get("username").toString();
        Long userId = Long.valueOf(paramBodyMap.get("userId").toString());
        if (result){
            ErpOrderLogEntity erpOrderLogEntity = new ErpOrderLogEntity();
            erpOrderLogEntity.setCreateBy(userId.toString());
            erpOrderLogEntity.setCreateName(username);
            erpOrderLogEntity.setCreateTime(new Date());
            erpOrderLogEntity.setOrderNo(erpOrder.getOrderNo());
            erpOrderLogEntity.setLogText("修改订单收货人信息");
            erpOrderLogService.insert(erpOrderLogEntity);
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] orderNos){
        erpOrderService.deleteBatchIds(Arrays.asList(orderNos));

        return R.ok();
    }

    /**
     * 确认付款备注修改
     */
    @RequestMapping("/receiveRemark")
    public R receiveRemark(@RequestParam("orderNo") String orderNo, @RequestParam("receiveMoneyRemark")String receiveMoneyRemark){
        return erpOrderService.receiveRemark(orderNo,receiveMoneyRemark);
    }

    /**
     * 财务订单计入成本统计
     */
    @RequestMapping("/costRecordConfirm")
    public R costRecordConfirm(@RequestParam("orderNo") String orderNo, @RequestParam("costRecord")Integer costRecord){
        return erpOrderService.costRecordConfirm(orderNo,costRecord);
    }

    /**
     * 订单分配
     * @param orderAllotDto
     * @return
     */
    @RequestMapping("/allotOrder")
    public R allotOrder(@RequestBody OrderAllotDto orderAllotDto){
        return erpOrderService.allotOrder(orderAllotDto);
    }

    /**
     * 订单商品分配
     * @param orderItemAllotDto
     * @return
     */
    @RequestMapping("/allotOrderItem")
    public R allotOrderItem(@RequestBody OrderItemAllotDto orderItemAllotDto){
        return erpOrderService.allotOrderItem(orderItemAllotDto);
    }

    /**
     * 删除分配信息
     * @param productionId
     * @return
     */
    @RequestMapping("/deleteAllotItem")
    public R deleteAllotItem(@RequestParam("productionId") Integer productionId, @RequestParam("orderId") String orderId){
        return erpOrderService.deleteAllotItem(productionId, orderId);
    }

}
